//
//  XUITableViewController.m
//  etionUI
//
//  Created by wu jingxing on 13-3-30.
//  Copyright (c) 2013年 GuangZhouXuanWu. All rights reserved.
//

#import "XUITableViewController.h"

#import "UIStyleSkinManager.h"

#import "UIStyleSkinManagerInternal.h"

@interface XUITableViewController ()
{
}
@end

@implementation XUITableViewController

@synthesize ctable = _ctable;

- (void)createTableView:(XUITabelViewStyleType)style
{
    CGRect frame = self.view.bounds;
    
//    BOOL mixSearchRefresh = NO;
//    
//    if((style & (XUITabelViewStyleTypeSearch|XUITabelViewStyleTypeRefresh)) == (XUITabelViewStyleTypeSearch|XUITabelViewStyleTypeRefresh))
//    {
//        mixSearchRefresh = YES;
//    }

    [self createTableView:frame style:style];
    
//    if(mixSearchRefresh == YES )
//    {
//        if(self.automaticallyAdjustsScrollViewInsets == YES)
//        {
//            UIEdgeInsets edge = _ctable.contentInset;
//            edge.top += 44;
//            _ctable.contentInset = edge;
//        }
//        else
//        {
//            _ctable.top -= 64;
//            _ctable.height -= 64;
//        }
//        
//        if(mixSearchRefresh == YES)
//        {
//            XUISearchBar *searchBar = [(XUISearchBar *)[_ctable.tableHeaderView retain_mm] autorelease_mm];
//            _ctable.tableFooterView = nil;
//            
//            
//        }
//    }
}

- (void)createTableView:(CGRect)frame style:(XUITabelViewStyleType)style
{
    if (XUITabelViewStyleTypeNone == (XUITabelViewStyleTypeNone & style))
    {
        _ctable = (XUITableView *) [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        _ctable.delegate = self;
        _ctable.dataSource = self;
    }
    else
    {
        UITableViewStyle tstyle = (XUITabelViewStyleTypePlain == (XUITabelViewStyleTypePlain & style)) ? UITableViewStylePlain :
        (XUITabelViewStyleTypeGroup == (XUITabelViewStyleTypeGroup & style)) ? UITableViewStyleGrouped : UITableViewStylePlain;
        _ctable = [[XUITableView alloc] initWithFrame:frame style:tstyle];
        _ctable.delegate = self;
        _ctable.dataSource = self;
        if (XUITabelViewStyleTypeGroup == (XUITabelViewStyleTypeGroup & style))
        {
            _ctable.backgroundView = nil;
        }
        if (XUITabelViewStyleTypeRefresh == (XUITabelViewStyleTypeRefresh & style))
        {
            [_ctable createRefreshHead];
        }
        if (XUITabelViewStyleTypeSearch == (XUITabelViewStyleTypeSearch & style))
        {
            [_ctable createSearchBar];
            _ctable.searchBar.delegate = (id<XUISearchBarDelegate>)self;
        }
        if (XUITableViewStyleTypeLoadMore == (XUITableViewStyleTypeLoadMore & style))
        {
            [_ctable createLoadMoreView];
        }
    }
    _ctable.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    if (@available(iOS 11.0, *))
    {
        _ctable.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        CGFloat top = _ctable.contentInset.top+ [XUINavigationController navigationController].navigationBar.bottom;
        CGFloat left = _ctable.contentInset.left;
        CGFloat bottom = _ctable.contentInset.bottom;
        CGFloat right = _ctable.contentInset.right;
        _ctable.contentInset = UIEdgeInsetsMake(top,left,bottom,right);
        _ctable.scrollIndicatorInsets = UIEdgeInsetsMake(top,left,bottom,right);
    }

    [self.view insertSubview:_ctable atIndex:0];
}

- (void)fixedSearchBar
{
    if(_ctable.tableHeaderView != nil && [_ctable.tableHeaderView isKindOfClass:[XUISearchBar class]])
    {
        XUISearchBar *searchBar = [(XUISearchBar *)[_ctable.tableHeaderView retain_mm] autorelease_mm];
        _ctable.tableHeaderView = nil;
        
        CGFloat t = 0;
        
        if(self.automaticallyAdjustsScrollViewInsets == YES)
        {
            UIEdgeInsets edge = _ctable.contentInset;
            edge.top += 44;
            _ctable.contentInset = edge;
            _ctable.scrollIndicatorInsets = edge;
            _ctable.contentOffset = CGPointMake(0, -44);
        }
        else
        {
            _ctable.top += 64;
            _ctable.height -= 64;
            
            if(self.navigationController.navigationBarHidden == NO)
            {
                _ctable.top += 44;
                _ctable.height -= 44;
            }
        }
        
        if(self.navigationController.navigationBarHidden == YES)
            t = 0;
        else
            t = 64;
        
        searchBar.top = t;
        [self.view insertSubview:searchBar aboveSubview:_ctable];
    }
}

- (void)setAdjustTableViewInset:(UIEdgeInsets)adjustTableViewInset
{
    _ctable.contentInset = adjustTableViewInset;
    _ctable.scrollIndicatorInsets = adjustTableViewInset;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_ctable loadMoreViewRefreshScrollViewDidScroll:_ctable];
    //    [self tableViewBaseViewControllerDidScroll:self];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [_ctable loadMoreViewRefreshScrollViewDidEndDragging:_ctable];
    //    [self tableViewBaseViewControllerDidEndDragging:self willDecelerate:decelerate];
}

//- (void)tableViewBaseViewControllerDidScroll:(CTableViewBaseViewController *)viewController
//{
//    [_ctable loadMoreViewRefreshScrollViewDidScroll:_ctable];
//}

//- (void)tableViewBaseViewControllerDidEndDragging:(CTableViewBaseViewController *)viewController willDecelerate:(BOOL)bDecelerate
//{
//    [_ctable loadMoreViewRefreshScrollViewDidEndDragging:_ctable];
//}

- (void)tableView:(XUITableView *)tableview refreshTableContent:(void (^)())asyncEndRefreshing
{
}

- (BOOL)loadingMoreContentOfTableView:(XUITableView *)tableview
{
    return _bLoadmoring;
}

-(void)loadMoreContentOfTableView:(XUITableView *)tableview
{
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if ([_ctable respondsToSelector:@selector(setSeparatorInset:)])
    {
        [_ctable setSeparatorInset:UIEdgeInsetsZero];
    }
    
#ifdef __IPHONE_8_0
    if ([_ctable respondsToSelector:@selector(setLayoutMargins:)])
    {
        [_ctable setLayoutMargins:UIEdgeInsetsZero];
    }
#endif
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {
        UIEdgeInsets edge = UIEdgeInsetsZero;
        if([_ctable.delegate respondsToSelector:@selector(tableView:setSeparatorInsetForRowAtIndexPath:)])
            edge = [_ctable.delegate tableView:_ctable setSeparatorInsetForRowAtIndexPath:indexPath];
        [cell setSeparatorInset:edge];
    }
    
#ifdef __IPHONE_8_0
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
#endif
}

-(void)beginPullRefreshView:(XMSDataOperationManager *)dom
{
    [_ctable showRefreshView];
    
    _bRefreshing = YES;
}

-(void)beginPullRefreshView:(XMSDataOperationManager *)dom funcationName:(NSString *)szFuncationname
{
    [_ctable showRefreshView];
    
    _bRefreshing = YES;
}

-(void)endPullRefreshView:(XMSDataOperationManager *)dom
{
    [_ctable hideRefreshView];
    
    _bRefreshing = NO;
}

-(void)endPullRefreshView:(XMSDataOperationManager *)dom funcationName:(NSString *)szFuncationname
{
    [_ctable hideRefreshView];
    
    _bRefreshing = NO;
}


- (void)beginPullLoadMoreView:(XMSDataOperationManager *)dom
{
    [_ctable showLoadMoreStatus];
    
    _bLoadmoring = YES;
}

- (void)beginPullLoadMoreView:(XMSDataOperationManager *)dom funcationName:(NSString *)szFuncationname
{
    [_ctable showLoadMoreStatus];
    
    _bLoadmoring = YES;
}

- (void)endPullLoadMoreView:(XMSDataOperationManager *)dom
{
    [_ctable hideLoadMoreStatus];
    
    _bLoadmoring = NO;
}

- (void)endPullLoadMoreView:(XMSDataOperationManager *)dom funcationName:(NSString *)szFuncationname
{
    [_ctable hideLoadMoreStatus];
    
    _bLoadmoring = NO;
}

- (void)dealloc
{
    _ctable.delegate = nil;
    _ctable.dataSource = nil;
    ReleaseObj(_ctable);
    superdealloc_mm;
}
@end
